﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteButton : MonoBehaviour {

    public enum ButtonType
    {
        Battery,
        Star,
        Comunication,
        Others
    }
    [SerializeField]
    public ButtonType Type;
    public int ID;

    GameObject BuyWindow;
    GameObject StarWindow;
    void Start()
    {
        Normal();
        BuyWindow = GameObject.Find("Canvas").transform.Find("ConstructPanel").gameObject;
        StarWindow = GameObject.Find("Canvas").transform.Find("StatPanel").gameObject;
    }
    void Update()
    {
        if (GetComponent<PlanetState>() != null)
            if (GetComponent<PlanetState>().Owner == ControlCenter.Instance.PlayerIdNow)
                return;
        UIMoni();
    }
    void UIMoni()
    {
        Vector2 pos = this.transform.position;
        Vector2 pla = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        float dist = (pos - pla).magnitude;
        if (dist < 0.5f)
            OnHover();
        else
            Normal();
    }
    void Normal()
    {
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0.5f);
    }
    void OnHover()
    {
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1f);
        if (Input.GetMouseButtonDown(0))
            OnClick();
    }
    void OnClick()
    {
        switch (Type)
        {
            case ButtonType.Battery:
                BuyWindow.SetActive(true);
                CanvasPosChange.Instance.ChangePos(gameObject, BuyWindow);
                BuyManager.Instance.BatteryID = ID;
                BuyManager.Instance.TargetPlanet = gameObject;
                break;
            case ButtonType.Star:
                StarWindow.SetActive(true);
                CanvasPosChange.Instance.ChangePos(gameObject, StarWindow);
                InteractStarManager.Instance.TargetStar = gameObject;
                break;
            case ButtonType.Comunication:
                if (ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] - 1 < 0)
                    return;
                transform.root.GetComponent<PlanetInteract>().Contact();
                break;
            default: break;
        }
    }
}
